//
// Created by kriko on 2021/7/8.
//

#include <csapp.h>

void str_cli(FILE* fp, int sockfd);

int main(int argc, char* argv[]) {
    int sockfd;
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <ip> <port>\n", argv[0]);
        return -1;
    }
    sockfd = Open_clientfd(argv[1], atoi(argv[2]));

    str_cli(stdin, sockfd);

    return 0;
}

void str_cli(FILE* fp, int sockfd) {
    char sendLine[MAXLINE], recvLine[MAXLINE];
    rio_t rio;

    Rio_readinitb(&rio, sockfd);
    while(Fgets(sendLine, MAXLINE, fp) != NULL) {
        write(sockfd, sendLine, strlen(sendLine));

        if (rio_readlineb(&rio, recvLine, MAXLINE) == 0) {
            unix_error("str_cli: server terminated prematurely");
            return;
        }
        Fputs(recvLine, stdout);
    }
}